<?php

namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use think\Db;

class PartnerController extends AdminBaseController{
	/**
	 * 合作伙伴列表
	 */
	public function index()
	{
		$user_name = input('user_name');
		$mobile = input('mobile');

		$where = [];
		if ($user_name) {
			$where['a.user_name'] = ['like',$user_name.'%'];
		}
		if ($mobile) {
			$where['a.mobile'] = ['like',$mobile.'%'];
		}
		$list = Db::name('partner')->alias('a')->field('a.*,b.oneself_volume,b.first_volume,b.second_volume')->join('shop_user b','a.user_id=b.user_id')->where($where)->order('a.pn_id DESC')->paginate(10,false,['query'=>request()->param()]);

		$level = config('member_level');
		$page = $list->render();

		$this->assign('level', $level);
		$this->assign('page', $page);
		$this->assign('list', $list);
		$this->assign('user_name', $user_name);
		$this->assign('mobile', $mobile);
		return $this->fetch();
	}
	/**
	 * 通过
	 */
	public function adoptStatus()
	{
		$pn_id = input('pn_id/d');
		if (empty($pn_id)) {
			$this->error('参数错误');
		}

		$info = Db::name('partner')->where('pn_id',$pn_id)->find();
		if (empty($info)) {
			$this->error('参数错误');
		}
		if ($info['status'] != 1) {
			$this->error('已不是待审核状态，无法修改');
		}
		$address = Db::name('region')->where('id',$info['address_id'])->find();
		if ($address['user_id']) {//判断地址是否已经被申请
			$this->error('地址已经被申请');
		}
		Db::startTrans();
		try {
			//修改状态
			Db::name('partner')->where('pn_id', $pn_id)->update(['status' => 2]);
			//地址增加代理
			Db::name('region')->where('id',$info['address_id'])->update(['user_id' => $info['user_id']]);
			//去除代理商本来的地址
			Db::name('region')->where('id','neq',$info['address_id'])->where('user_id',$info['user_id'])->update(['user_id' => 0]);
			//其他的申请设置拒绝
			Db::name('partner')->where('address_id',$info['address_id'])->where('status', 1)->update(['status' => 3]);
			//升级
			Db::name('shop_user')->where('user_id',$info['user_id'])->update(['level' => $info['apply_level']]);
			Db::commit();
			$this->success('操作成功');
		} catch (Exception $e) {
			Db::rollback();
			$this->error('操作失败');
		}	
	}
	/**
	 * 拒绝
	 */
	public function refuseStatus()
	{
		$pn_id = input('pn_id/d');
		if (empty($pn_id)) {
			$this->error('参数错误');
		}

		$info = Db::name('partner')->where('pn_id',$pn_id)->find();
		if (empty($info)) {
			$this->error('参数错误');
		}
		if ($info['status'] != 1) {
			$this->error('已不是待审核状态，无法修改');
		}

		$res = Db::name('partner')->where('pn_id', $pn_id)->update(['status' => 3]);
		if (false !== $res) {
			$this->success('操作成功');
		} else {
			$this->error('操作失败');
		}
	}
	/**
	 * 详情
	 */
	public function detail()
	{
		$pn_id = input('pn_id/d');
		if (empty($pn_id)) {
			$this->error('获取失败');
		}

		$level = config('member_level');
		$detail = Db::name('partner')->alias('a')->field('a.*,b.volume,b.oneself_volume,b.first_volume,b.second_volume')->join('shop_user b', 'a.user_id=b.user_id')->where('a.pn_id',$pn_id)->find();
		$detail['address'] = $this->getAddress($detail['address_id'],$detail['apply_level']);
		$detail['apply_level'] = $level[$detail['apply_level']];
		$this->assign('detail', $detail);
		return $this->fetch();	
	}
	/**
	 * 获取地址
	 */
	public function getAddress($address_id,$level)
	{
		switch ($level) {
			case '3':
				$info = Db::name('region')->alias('a')->field('a.name as area,b.name as city,c.name as province')->join('region b','a.parent_id=b.id')->join('region c','b.parent_id=c.id')->where('a.id',$address_id)->find();
				return $info['province'].' '.$info['city'].' '.$info['area'];
				break;
			case '4':
				$info = Db::name('region')->alias('a')->field('a.name as city,b.name as province')->join('region b','a.parent_id=b.id')->where('a.id',$address_id)->find();
				return $info['province'].' '.$info['city'];
				break;
			case '5':
				$info = Db::name('region')->where('id',$address_id)->value('name');
				return $info;
				break;
			default:
				return '';
				break;
		}
	}
}